package com.goods.shop.service.wxmessage;

import com.goods.shop.service.AbstractService;
import com.goods.shop.service.constants.WeiXinUrlFinals;
import com.goods.shop.service.model.WxArticle;
import com.goods.shop.service.model.wxmessage.WxEvent;
import com.goods.shop.service.model.wxmessage.WxMsgType;
import com.goods.shop.service.model.wxmessage.WxTextModel;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class WxMessageServiceImpl extends AbstractService implements WxMessageService {

    @Override
    public WxTextModel generateText(Element root) {
        WxTextModel entity=new WxTextModel();
        String toUserName = root.getElementsByTagName("ToUserName").item(0).getTextContent();
        String fromUserName = root.getElementsByTagName("FromUserName").item(0).getTextContent();
        String msgType=root.getElementsByTagName("MsgType").item(0).getTextContent();
        WxMsgType wxMsgType=WxMsgType.valueOf(msgType.toUpperCase());

        String eventKey="";
        NodeList list=root.getElementsByTagName("EventKey");
        if(null!=list){
            eventKey= list.item(0).getTextContent();
        }

        entity.setFromUserName(toUserName);
        entity.setToUserName(fromUserName);
        entity.setMsgType(wxMsgType);
        long time=System.currentTimeMillis()/1000;
        entity.setCreateTime(String.valueOf(time));
        entity.setMsgId(String.valueOf(System.nanoTime()));
        switch(wxMsgType){
            case TEXT:
                String content = root.getElementsByTagName("Content").item(0).getTextContent();//用户发送的内容
                switch(content){
                    case "1"://1 回复文本消息
                        entity.setContent("文本:"+content);
                        break;
                    case "2"://2 回复图片消息
                        entity.setMsgType(WxMsgType.IMAGE);
                        entity.setMediaId("fr0MqP22tqmjis41MGSqQ52uoX1fiq6VQKVyTVN-l3S8Dr8AOMMuk4DW1mEqGMO1");//TODO
                        break;
                    case "3"://3 回复语音消息
                        entity.setMsgType(WxMsgType.VOICE);
                        entity.setMediaId("fr0MqP22tqmjis41MGSqQ52uoX1fiq6VQKVyTVN-l3S8Dr8AOMMuk4DW1mEqGMO1");//TODO
                        break;
                    case "4"://4 回复视频消息
                        entity.setMsgType(WxMsgType.VIDEO);
                        entity.setMediaId("fr0MqP22tqmjis41MGSqQ52uoX1fiq6VQKVyTVN-l3S8Dr8AOMMuk4DW1mEqGMO1");//TODO
                        entity.setTitle("视频标题 ");
                        entity.setDescription("视频说明");
                         break;
                    case "5"://5 回复音乐消息
                        entity.setMsgType(WxMsgType.MUSIC);
                        entity.setMediaId("fr0MqP22tqmjis41MGSqQ52uoX1fiq6VQKVyTVN-l3S8Dr8AOMMuk4DW1mEqGMO1");//TODO
                        entity.setTitle("音乐标题 ");
                        entity.setDescription("音乐说明");
                        entity.setMusicUrl("http://94.191.88.231/mp3/2017/02/Five.mp3");
                        entity.setHqMusicUrl("http://94.191.88.231/mp3/2017/02/Five.mp3");
                        break;
                    case "6"://6 回复图文消息
                        entity.setMsgType(WxMsgType.NEWS);
                        break;
                    default:break;
                }
                break;
            case EVENT:
                String event= root.getElementsByTagName("Event").item(0).getTextContent();

                WxEvent wxEvent=WxEvent.valueOf(event.toUpperCase());
                entity.setEvent(wxEvent);
                entity.setEventKey(eventKey);//事件
                switch(wxEvent){
                    case CLICK:
                        eventKey = root.getElementsByTagName("EventKey").item(0).getTextContent();//用户发送的内容
                        entity.setEventKey(eventKey);
                        entity.setMsgType(WxMsgType.NEWS);
                        entity.setContent(entity.getContent()+"-"+wxMsgType.getDesc());
                        break;
                    case SUBSCRIBE://关注
                        entity.setMsgType(WxMsgType.TEXT);
                        entity.setContent("谢谢关注，"+wxEvent.getDesc()+",欢迎进入..."+",eventKey=<a href='http://www.baidu.com'>"+entity.getEventKey()+"</a>");
                        break;
                    case UNSUBSCRIBE://取消关注
                        entity.setMsgType(WxMsgType.TEXT);
                        entity.setContent(wxEvent.getDesc());
                        logger.info("取消关注");
                        break;
                    case SCAN://扫描事件
                        entity.setMsgType(WxMsgType.TEXT);
                        eventKey=root.getElementsByTagName("EventKey").item(0).getTextContent();
                        if(eventKey.toUpperCase().equals(WeiXinUrlFinals.SCENE_VALUE)){
                            entity.setContent(wxEvent.getDesc()+",之前关注过该公众号,eventKey="+eventKey);
                        }else{
                            entity.setContent(wxEvent.getDesc()+",首次关注,"+eventKey);
                        }
                        logger.info(wxEvent.getDesc()+":"+entity.getContent());
                        break;
                    case LOCATION:
                        String latitude=root.getElementsByTagName("Latitude").item(0).getTextContent();//纬度
                        String longitude=root.getElementsByTagName("Longitude").item(0).getTextContent();//经度
                        entity.setContent(wxEvent.getDesc()+",位置（"+latitude+","+longitude+")");
                        break;
                    case VIEW:
                        eventKey=root.getElementsByTagName("EventKey").item(0).getTextContent();//跳转的路径
                        entity.setContent(wxEvent.getDesc()+",跳转的URL（"+eventKey+")");
                        break;
                }
                break;
        }
//        entity.setContent(entity.getContent()+"-"+wxMsgType.getDesc());
        return entity;
    }

    /**
     * 这个方法用于返回响应文本和点击事件返回的情况
     * @param entity
     * @return
     */
    @Override
    public String responseText(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
        responseMsg.append("<Content><![CDATA[").append(entity.getContent()).append("]]></Content>");
        responseMsg.append("<MsgId>").append(entity.getMsgId()).append("</MsgId>");//消息id，随便填，但位数要够
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String responseImage(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[image]]></MsgType>");
        responseMsg.append("<Image><MediaId><![CDATA[").append(entity.getMediaId()).append("]]></MediaId></Image>");
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String responseVoice(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[voice]]></MsgType>");
        responseMsg.append("<Voice><MediaId><![CDATA[").append(entity.getMediaId()).append("]]></MediaId></Voice>");
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String responseVideo(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[video]]></MsgType>");

        responseMsg.append("<Video>");
        responseMsg.append("<MediaId><![CDATA[").append(entity.getMediaId()).append("]]></MediaId>");
        responseMsg.append("<Title><![CDATA[").append(entity.getTitle()).append("]]></Title>");
        responseMsg.append("<Description><![CDATA[").append(entity.getDescription()).append("]]></Description>");
        responseMsg.append("</Video>");

        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String responseMusic(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[music]]></MsgType>");

        responseMsg.append("<Music>");
        responseMsg.append("<Title><![CDATA[").append(entity.getTitle()).append("]]></Title>");
        responseMsg.append("<Description><![CDATA[").append(entity.getDescription()).append("]]></Description>");
        responseMsg.append("<MusicUrl><![CDATA[").append(entity.getMusicUrl()).append("]]></MusicUrl>");
        responseMsg.append("<HQMusicUrl><![CDATA[").append(entity.getHqMusicUrl()).append("]]></HQMusicUrl>");
        responseMsg.append("<ThumbMediaId><![CDATA[").append(entity.getMediaId()).append("]]></ThumbMediaId>");
        responseMsg.append("</Music>");

        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String repsonseNews(WxTextModel entity, List<WxArticle> articles) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(entity.getCreateTime()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[news]]></MsgType>");
        responseMsg.append("<ArticleCount>").append(articles.size()).append("</ArticleCount>");
        responseMsg.append("<Articles>");
        for(WxArticle article:articles){
            responseMsg.append("<item>");
            responseMsg.append("<Title><![CDATA[").append(article.getTitle()).append("]]></Title>");
            responseMsg.append("<Description><![CDATA[").append(article.getDescription()).append("]]></Description>");
            responseMsg.append("<PicUrl><![CDATA[").append(article.getPicUrl()).append("]]></PicUrl>");
            responseMsg.append("<Url><![CDATA[").append(article.getUrl()).append("]]></Url>");
            responseMsg.append("</item>");
        }
        responseMsg.append("</Articles>");
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    public static void main(String[] args) {
        String line=null;
        int count=0;
        BufferedReader reader=null;
        String file="G:\\sql\\sql-detail-1\\sql-detail-1.txt";//TODO
        try {
            reader=new BufferedReader(new FileReader(new File(file)));
            while((line=reader.readLine())!=null){
                count++;
                if(count%5000000==0){
                    System.out.println("已经到了"+count);
                }
            }
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
